package com.ktjy.ch15.service.impl;

import com.ktjy.ch15.entity.StorageRecord;
import com.ktjy.ch15.mapper.StorageRecordMapper;
import com.ktjy.ch15.service.StorageRecordService;
import com.ktjy.ch15.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("storageRecordService")
public class StorageRecordServiceImpl implements StorageRecordService {

    @Autowired
    StorageRecordMapper storageRecordMapper;

    @Override
    public Page<StorageRecord> findPage(StorageRecord storageRecord, Integer pageNo, Integer pageSize) {
        Page<StorageRecord> page = new Page<StorageRecord>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        // 查询总数，计算总页数
        Integer count = storageRecordMapper.count(storageRecord);
        page.setTotalCount(count);
        // 查询分页数据
        Integer pageBegin=(pageNo-1)*pageSize;
        List<StorageRecord> data = storageRecordMapper.getPage(storageRecord, pageBegin, pageSize);
        page.setData(data);
        return page;
    }

    @Override
    public boolean save(StorageRecord storageRecord) {
        return storageRecordMapper.insert(storageRecord) > 0;
    }

    @Override
    public StorageRecord findById(Long id) {
        return storageRecordMapper.getById(id);
    }

    @Override
    public boolean update(StorageRecord storageRecord) {
        return storageRecordMapper.update(storageRecord) > 0;
    }

    @Override
    public boolean delete(Long id) {
        return storageRecordMapper.delete(id) > 0;
    }

    @Override
    public StorageRecord findBySrCode(String srCode) {
        return storageRecordMapper.getBySrCode(srCode);
    }
}
